package com.android.vgongyi.dao;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.entity.CommentEntity;

public class CommentDAO extends BaseDAO{
	private String commentId         = "commentId";
	private String itemId            = "itemId";
	private String weibo_name        = "weibo_name";
	private String weibo_url         = "weibo_url";
	private String profile_imageurl  = "profile_imageurl";
	private String comment_text      = "comment_text";
	private String id                = "id";
	private String aid               = "aid";
	private String create_at         = "create_at";
	
	private Context context;
	
	public CommentDAO(Context context) {
		
		this.context = context;
		
	}

	@Override
	public String createTableString() {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("create table ");
		buffer.append(TABLE_COMMENT);
		buffer.append("(");
		buffer.append("_id integer primary key AUTOINCREMENT,");
		buffer.append(commentId).append(" text,");
		buffer.append(itemId).append(" text,");
		buffer.append(weibo_name).append(" text,");
		buffer.append(weibo_url).append(" text,");
		buffer.append(profile_imageurl).append(" text,");
		buffer.append(comment_text).append(" text,");
		buffer.append(id).append(" text unique,");
		buffer.append(aid).append(" text,");
		buffer.append(create_at).append(" text");
		buffer.append(");");
		return buffer.toString();
		
	}

	@Override
	public String dropTable() {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(super.DROP_TABLE);
		buffer.append(TABLE_COMMENT);
		
		return buffer.toString();
		
	}

	@Override
	public void insert(Object obj) {
		
		CommentEntity entity       = (CommentEntity)obj;
		ContentResolver resolver   = context.getContentResolver();
		ContentValues values       = new ContentValues();
		
		values.put(commentId, entity.getCommentId());
		values.put(itemId, entity.getItemId());
		values.put(weibo_name, entity.getWeibo_name());
		values.put(weibo_url, entity.getWeibo_url());
		values.put(profile_imageurl, entity.getProfile_imageurl());
		values.put(comment_text, entity.getComment_text());
		values.put(id, entity.getId());
		values.put(aid, entity.getAid());
		values.put(create_at, entity.getCreate_at());
		
		resolver.insert(URI_COMMENT, values);
		
	}

	@Override
	public void delete(String where, String[] selectionArgs) {
		
		ContentResolver resolver = context.getContentResolver();
		resolver.delete(URI_COMMENT, where, selectionArgs);
		
	}

	@Override
	public ArrayList<CommentEntity> query(String[] selections, String where,String[] selectionArgs,String sortOrder) {
		
		ContentResolver resolver           = context.getContentResolver();
		Cursor cursor                      =  resolver.query(URI_COMMENT, selections, where, selectionArgs, null);
		ArrayList<CommentEntity> arrayList = new ArrayList<CommentEntity>();		
		CommentEntity entity               = null;
		
		if(null==cursor) return arrayList;
		
		if(cursor.getCount()<AppConstant.NO_1){
			
			cursor.close();
			return arrayList;
			
		}
		
		int count = cursor.getCount();
		
		if(count>AppConstant.NO_0) cursor.moveToFirst();
		
		do {
		
			entity = new CommentEntity();
			
			entity.setCommentId(cursor.getString(cursor.getColumnIndex(commentId)));
			entity.setItemId(cursor.getString(cursor.getColumnIndex(itemId)));
			entity.setWeibo_name(cursor.getString(cursor.getColumnIndex(weibo_name)));
			entity.setWeibo_url(cursor.getString(cursor.getColumnIndex(weibo_url)));
			entity.setProfile_imageurl(cursor.getString(cursor.getColumnIndex(profile_imageurl)));
			entity.setComment_text(cursor.getString(cursor.getColumnIndex(comment_text)));
			entity.setId(cursor.getString(cursor.getColumnIndex(id)));
			entity.setAid(cursor.getString(cursor.getColumnIndex(aid)));
			entity.setCreate_at(cursor.getString(cursor.getColumnIndex(create_at)));
			
			arrayList.add(entity);
			
		} while (cursor.moveToNext());
		
		cursor.close();
		
		return arrayList;
	}

	@Override
	public void update(Object obj, String where, String[] selectionArgs) {
		
		CommentEntity entity     = (CommentEntity )obj;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values     = new ContentValues();
		
		values.put(commentId, entity.getCommentId());
		values.put(itemId, entity.getItemId());
		values.put(weibo_name, entity.getWeibo_name());
		values.put(weibo_url, entity.getWeibo_url());
		values.put(profile_imageurl, entity.getProfile_imageurl());
		values.put(comment_text, entity.getComment_text());
		values.put(id, entity.getId());
		values.put(aid, entity.getAid());
		values.put(create_at, entity.getCreate_at());
		
		resolver.update(URI_COMMENT, values, where, selectionArgs);
		
	}

}
